﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admintool.ExtensionMethods
{
    public static class DateTimeExtension
    {
        public static long ToUnixTimestamp(this DateTime source)
        {
            //create Timespan by subtracting the value provided from the Unix Epoch
            TimeSpan span = (source - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
            return (long)span.TotalSeconds;
        }

        public static DateTime ToDateTime(this long timestamp)
        {
            //create a new DateTime value based on the Unix Epoch
            DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            //add the timestamp to the value
            DateTime newDateTime = converted.AddSeconds(timestamp);

            //return the value in string format
            return newDateTime.ToLocalTime();
        }
    }
}
